package com.freesia.crypt.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.alibaba.fastjson.JSONObject;
import com.freesia.constant.Constants;
import com.freesia.crypt.exception.CryptException;
import com.freesia.util.UEmpty;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.Security;

/**
 * @author Evad.Wu
 * @Description 加密/解密 工具类
 * @date 2024-03-15
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UCrypt {
    public static final String KEY = "KysrKytqamtrbGw7JzEyMw==";
    public static final String ALGORITHM = SymmetricAlgorithm.AES.getValue();
    public static final String PADDING = "AES/ECB/ZeroPadding";
    public static AES aes;

    static {
        Security.addProvider(new BouncyCastleProvider());
        initAes();
    }

    /**
     * 初始化AES秘钥
     */
    private static void initAes() {
        if (UEmpty.isEmpty(KEY)) {
            throw new CryptException("crypt.aes.key.required", new Object[]{});
        }
//        System.out.println("AES KEY：" + key);
        aes = new AES(Mode.ECB, Padding.PKCS5Padding, KEY.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * RSA 公钥加密
     *
     * @param pubKey 公钥
     * @param data   待加密数据
     * @return 加密数据
     */
    public static String rsaEncrypt(String pubKey, String data) {
        return new RSA(null, pubKey).encryptBase64(data, KeyType.PublicKey);
    }

    /**
     * RSA 私钥解密
     *
     * @param priKey 私钥
     * @param data   待解密数据
     * @return 解密数据
     */
    public static String rsaDecrypt(String priKey, String data) {
        return new RSA(priKey, null).decryptStr(data, KeyType.PrivateKey);
    }

    /**
     * 使用默认的key和iv加密
     *
     * @param data 明文
     * @return 加密后的密文
     */
    public static String aesEncrypt(String data) {
        return aes.encryptBase64(data);
    }

    /**
     * 使用默认的key和iv解密
     *
     * @param data 密文
     * @return 解密后的明文
     */
    public static String aesDecrypt(String data) {
        return aes.decryptStr(data, StandardCharsets.UTF_8);
    }

    /**
     * 解密前端传来的报文，转为为对象
     *
     * @param requestBody 请求报文
     * @param clz         待转换的对象类型
     * @param <T>         对象泛型
     * @return 转换后的对象类型
     */
    public static <T> T aesDecryptJSON(String requestBody, Class<T> clz) {
        String encrypt = JSONObject.parseObject(requestBody).getString(Constants.ENCRYPT);
        String decrypt = aesDecrypt(encrypt);
        return JSONObject.parseObject(decrypt, clz);
    }

    /**
     * md5加密
     *
     * @param obj 入参
     * @param <T> {@link Object,File,InputStream}
     * @return 加密密文
     */
    public static <T> String md5Encrypt(T obj) {
        if (obj instanceof String) {
            return SecureUtil.md5((String) obj);
        } else if (obj instanceof File) {
            return SecureUtil.md5((File) obj);
        } else if (obj instanceof InputStream) {
            return SecureUtil.md5((InputStream) obj);
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String json = "{\"accountStatus\":\"1\",\"avatar\":\"avatar/DDF.png\",\"buildIn\":false,\"createTime\":\"2023-08-15 08:52:26\",\"creator\":\"Evad\",\"deptId\":\"1697116611017363459\",\"email\":\"1814926857@qq.com\",\"gender\":\"M\",\"id\":\"1\",\"logicDel\":false,\"modifier\":\"admin\",\"modifyTime\":\"2024-02-19 17:56:42\",\"nickName\":\"Dungeon Master\",\"password\":\"$2a$10$/QC8MgeOJWaymIe61PWKCO6SvstpPVpihq6I1iaLAs/y2aNu6CiC2\",\"recVer\":\"7\",\"remark\":\"My name is Van, I'm an artist, a performance artist, I'm hired for people to fulfill their fantasies, the Deep Dark Fantasies.\",\"telNo\":\"18878269603\",\"userName\":\"admin\",\"userType\":\"sys_user\"}";
//        final String encryptBase64 = aesEncrypt("666");
//        System.out.println(encryptBase64);
        String base64 =
//                "b6Gzu7Y+opaihJ0raRLwLViEGz6kDtyYzudQ4TniLgYNxVP5pcs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9jMu3XqibQ1B0BXdYPR/JQDBuHpG10gccn7R97YJY1q8eJiFq6dirT2xohtwqn5kM8iS2aNXY7VN6Ki8PXdgIBnWPx18lAot13nZKKqC63xIT5C24QWM=";
        final String aesDesEncrypt = aesDecrypt(base64);
        System.out.println(aesDesEncrypt);


        String aesKey = "KysrKytqamtrbGw7JzEyMw==";
        System.out.println(Base64.decodeStr(aesKey));
    }
}
